package com.lxr0860.lxr0860production_test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.lxr0860.lxr0860production_test.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PcieItemAdapter extends BaseAdapter {
    private List<String> dataList = new ArrayList<>();
    private Context mContext;
    private itemOnclicklistener listener;


    public PcieItemAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        InnerHolder mHolder;
        if (convertView == null) {
            mHolder = new InnerHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.pcie_item, parent, false);
            mHolder.mTextView = convertView.findViewById(R.id.text);
            convertView.setTag(mHolder);
        }else{
            mHolder = (InnerHolder) convertView.getTag();
        }
        mHolder.mTextView.setText(dataList.get(position));
        mHolder.mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClickListener(position);
                }
            }
        });
        return convertView;
    }

    public class InnerHolder{
        TextView mTextView;
    }

    public void setDataList(String[] data){
        this.dataList = Arrays.asList(data);

    }
    public void setItemOnclicklistener(itemOnclicklistener listener){
        this.listener = listener;

    }

    public   interface itemOnclicklistener{
        void onItemClickListener(int position);
    }
}
